// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 */
#include <linux/of.h>
#include <linux/regmap.h>
#include <mach/csp.h>
#include <mach/lombo_pmc.h>
#include "../regulator.h"

#define DISCRETE_DCDC0_CTRL	0x1
#define DISCRETE_DCDC1_CTRL	0x2
#define DISCRETE_DCDC2_CTRL	0x4

#define DISCRETE_DCDC0_EN_MASK	0x0001
#define DISCRETE_DCDC1_EN_MASK	0x0002
#define DISCRETE_DCDC2_EN_MASK	0x0004

static void __iomem *pm_reg_base;

enum {
	DISCRETE_ID_DCDC0,
	DISCRETE_ID_DCDC1,
	DISCRETE_ID_DCDC2,
};

struct regulator_desc lombo_regulators[] = {
	DISCRETE_LDO_REG("DCDC0", DISCRETE, DCDC0, "null"),
	DISCRETE_LDO_REG("DCDC1", DISCRETE, DCDC1, "null"),
#ifdef CONFIG_ARCH_LOMBO_N7V3
	DISCRETE_LDO_REG("DCDC2", DISCRETE, DCDC2, "null"),
#endif
};

int __get_regulators_count(void)
{
	return ARRAY_SIZE(lombo_regulators);
}

struct regulator_desc *__get_regulator_desc(void)
{
	return lombo_regulators;
}

int __lombo_read_regulator_data(void *context,
	unsigned int reg, unsigned int *val)
{
	lombo_csp_pmc_get_pm_pe(pm_reg_base, val);

	return 0;
}

int __lombo_write_regulator_data(void *context,
	unsigned int reg, unsigned int val)
{
	lombo_csp_pmc_set_pm_pe(pm_reg_base, reg, val);

	return 0;
}

struct regmap_config pmic_regmap = {
	.name = "discrete",
	.reg_bits = 32,
	.val_bits = 32,
	.reg_read = __lombo_read_regulator_data,
	.reg_write = __lombo_write_regulator_data,
	.max_register = 0x0,
	.cache_type = REGCACHE_RBTREE,
};

int __pmic_init(struct platform_device *pdev)
{
	struct resource *res;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		pr_err("%s:%d get resource error\n", __func__, __LINE__);
		return -EINVAL;
	}

	pm_reg_base = devm_ioremap_resource(&pdev->dev, res);
	if (pm_reg_base < 0) {
		pr_err("%s:%d, remap register error\n", __func__, __LINE__);
		return -EINVAL;
	}

	return 0;
}
